﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.OracleClient;
using dotnetCHARTING;
using ChartExtents;
using System.Drawing;
public partial class main_dataAnalyze_DTZR : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LineDAOs lineDAOs = LineDAOs.getInstance();
            List<String> lineNameList = lineDAOs.getLineNameList();
            for (int i = 0; i < lineNameList.Count; i++)
            {
                dropLine.Items.Add(lineNameList[i]);
            }
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            List<String> towerNameList = towerDAOs.getTowerNameList(lineNameList[0]);
            for (int j = 0; j < towerNameList.Count; j++)
            {
                dropTower.Items.Add(towerNameList[j]);
            }
            this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "日照");
            this.txtHJWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "环境温度");
            this.txtRZQD.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
            this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "日照强度");
            this.txtDXWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线温度");
            this.txtSSDL.Text = towerDAOs.getNow(dropTower.SelectedValue, "适时电流");
            this.txtXEDL.Text = towerDAOs.getNow(dropTower.SelectedValue, "限额电流");
            this.txtSSGL.Text = towerDAOs.getNow(dropTower.SelectedValue, "适时功率");
            this.txtRWXE.Text = towerDAOs.getNow(dropTower.SelectedValue, "限额功率");
        }
    }

    protected void dropLine_SelectedIndexChanged(object sender, EventArgs e)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        dropTower.Items.Clear();
        List<String> towerNameList = towerDAOs.getcorrespondingTowerNameList_DTZR(dropLine.SelectedValue);
        for (int j = 0; j < towerNameList.Count; j++)
        {
            dropTower.Items.Add(towerNameList[j]);
        }
        this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "日照");
        this.txtHJWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "环境温度");
        this.txtRZQD.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
        this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "日照强度");
        this.txtDXWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线温度");
        this.txtSSDL.Text = towerDAOs.getNow(dropTower.SelectedValue, "适时电流");
        this.txtXEDL.Text = towerDAOs.getNow(dropTower.SelectedValue, "限额电流");
        this.txtSSGL.Text = towerDAOs.getNow(dropTower.SelectedValue, "适时功率");
        this.txtRWXE.Text = towerDAOs.getNow(dropTower.SelectedValue, "限额功率");
    }
    //protected void btnHistory_Click(object sender, EventArgs e)
    //{
    //    TowerDAOs towerDAOs = TowerDAOs.getInstance();
    //    this.txtHJWD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "环境温度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtRZQD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "风速", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtFS.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "日照强度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtDXWD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "导线温度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtSSDL.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "适时电流", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtXEDL.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "限额电流", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtSSGL.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "适时功率", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtRWXE.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "限额功率", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    Chart[] chart = { this.Chart1, this.Chart2};
    //    for (int i = 0; i < 2; i++)
    //    {   
    //        Drawing(i);
    //    }

    //}
    private void Drawing(int i)
    {
        Chart[] chart = { this.Chart1, this.Chart2 };
        chart[i].Type = ChartType.Combo;//Horizontal;
        chart[i].Size = "600x350";
        chart[i].TempDirectory = "images";
        chart[i].Debug = true;
        chart[i].XAxis.TimeInterval = TimeInterval.Day;
        chart[i].XAxis.Scale = Scale.Time;
        SeriesCollection mySC;
        if (i == 0)
        {
            chart[i].YAxis.Name = "导线温度";
            mySC = GetDataSource();
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            DataSet ds = towerDAOs.getAlarm(dropTower.SelectedValue, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
            int j = ds.Tables[0].Rows.Count;
            String[] description = new String[j];
            for (int b = 0; b < j; b++)
            {
                description[b] = ds.Tables[0].Rows[b]["act_description"].ToString();
                foreach (Element myE in mySC[0].Elements)
                {
                    if (description[b].Equals("导线温度黄色告警"))
                        myE.Color = Color.Yellow;
                    else if (description[b].Equals("导线温度橙色告警"))
                        myE.Color = Color.Orange;
                    else if (description[b].Equals("导线温度红色告警"))
                        myE.Color = Color.Red;
                }
                chart[i].XAxis.Markers.Add(new AxisMarker("", Color.FromArgb(50, Color.Red), ds.Tables[0].Rows[b]["start_time"], ds.Tables[0].Rows[b]["stop_time"]));
            }
        }
        else
        {
            chart[i].YAxis.Name = "热稳限额&功率限额&实时功率";
            mySC = GetDataSourceB();
        }
        chart[i].DefaultSeries.Type = SeriesType.Spline;
        chart[i].TitleBox.Position = TitleBoxPosition.FullWithLegend;
        chart[i].Series.StartDate = RadDatePicker1.SelectedDate.Value.Date;
        chart[i].Series.EndDate = RadDatePicker2.SelectedDate.Value.Date;
        chart[i].SeriesCollection.Add(mySC);

    }
    private SeriesCollection GetDataSource()
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();
        DataSet ds;
        ds = towerDAOs.getSpineB(dropTower.SelectedValue, "导线温度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        int i = ds.Tables[0].Rows.Count;
        if (i != 0)
        {
            Series s = new Series();

            s.Name = "导线温度";
            for (int b = 0; b < i; b++)
            {
                Element e = new Element();
                e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                s.Elements.Add(e);
            }
            SC.Add(s);
            this.Chart1.Visible = true;
        }
        return SC;
    }
    private SeriesCollection GetDataSourceB()
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();
        DataSet ds = new DataSet();
        int i = -1;
        for (int a = 1; a <= 3; a++)
        {
            Series s = new Series();
            if (a == 1)
            {
                ds = towerDAOs.getSpineB(dropTower.SelectedValue, "热稳限额", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
                s.Name = "热稳限额";
            }
            if (a == 2)
            {
                ds = towerDAOs.getSpineB(dropTower.SelectedValue, "实时功率", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
                s.Name = "实时功率";
            }
            if (a == 3)
            {
                ds = towerDAOs.getSpineB(dropTower.SelectedValue, "功率限额", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
                s.Name = "功率限额";
            }

            i = ds.Tables[0].Rows.Count;
            if (i > 0)
            {
                if (i > 50)
                {
                    i = 50;
                }
                for (int b = 0; b < i; b++) //X轴尺度个数
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart2.Visible = true;
            }
        }
        return SC;
    }

    protected void dropTower_SelectedIndexChanged(object sender, EventArgs e)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "日照");
        this.txtHJWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "环境温度");
        this.txtRZQD.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
        this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "日照强度");
        this.txtDXWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线温度");
        this.txtSSDL.Text = towerDAOs.getNow(dropTower.SelectedValue, "适时电流");
        this.txtXEDL.Text = towerDAOs.getNow(dropTower.SelectedValue, "限额电流");
        this.txtSSGL.Text = towerDAOs.getNow(dropTower.SelectedValue, "适时功率");
        this.txtRWXE.Text = towerDAOs.getNow(dropTower.SelectedValue, "限额功率");
    }
    protected void btnJSFX_Click(object sender, EventArgs e)
    {
        Drawing(0);
        Drawing(1);
    }
}
